package cn.yunhe.insurance.util;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HideDataUtil {
    public static String hideCardNo(String creditCard) {
        if (StringUtils.isBlank(creditCard)) {
            return creditCard;
        }
        int length = creditCard.length();
        int beforeLength = 4;
        int afterLength = 4;
        //替换字符串，当前使用“*”
        String replaceSymbol = "*";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            if (i < beforeLength || i >= (length - afterLength)) {
                sb.append(creditCard.charAt(i));
            } else {
                sb.append(replaceSymbol);
            }
        }
        return sb.toString();
    }

    /**
     * 方法描述 隐藏手机号中间位置字符，显示前三后三个字符
     * @author MSJ
     * @date 2018年4月3日 上午10:38:51
     * @param phoneNo
     * @return
     */
    public static String hidePhoneNo(String phoneNo) {
        //phoneNo.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
        if (StringUtils.isBlank(phoneNo)) {
            return phoneNo;
        }
        int length = phoneNo.length();
        int beforeLength = 3;
        int afterLength = 3;
        //替换字符串，当前使用“*”
        String replaceSymbol = "*";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            if (i < beforeLength || i >= (length - afterLength)) {
                sb.append(phoneNo.charAt(i));
            } else {
                sb.append(replaceSymbol);
            }
        }
        return sb.toString();
    }
    public static String hideIdCard(String idCard){
       String card= idCard.replaceAll("(\\d{4})\\d{10}(\\d{4})","$1**********$2");
       return card;
    }
    public static String hideUserName(String name){
       int length=name.length();
        String replaceSymbol = "*";
        StringBuffer sb = new StringBuffer();
        int beforeLength = 1;
        for (int i = 0; i < length; i++) {
            if(length<=3){//名字是3个字
                if (i < beforeLength ) {
                    sb.append(name.charAt(i));
                } else {
                    sb.append(replaceSymbol);
                }
            }else if (length==4){
                if (i <=beforeLength ) {
                    sb.append(name.charAt(i));
                } else {
                    sb.append(replaceSymbol);
                }
            }
        }
        return sb.toString();
    }
   /* public static void main(String[] args) {
        String creditCard = "6212264100011335373";
        String str = hideCardNo(creditCard);
        System.out.println(str);
        String idCard = "421302199208165464";
        System.out.println(hideIdCard(idCard));
        String name="张森";
        System.out.println(hideUserName(name));
    }*/
}
